/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.additional;

import com.sun.lwuit.Component;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.plaf.UIManager;

/**
 *
 * @author Kvest
 */
public class TickerRenderer extends DefaultListCellRenderer
{
    private DefaultListCellRenderer selectedRenderer = new DefaultListCellRenderer(false);
    private List parentList;

    public TickerRenderer()
    {
        super(false);
    }

    public boolean animate()
    {
        if(isTickerEnabled() && parentList != null && parentList.getComponentForm() != null)
        {
            if(selectedRenderer.isTickerRunning())
            {
                if(selectedRenderer.animate())
                {
                    parentList.repaint();
                }
            }
        }
        return super.animate();
    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
    {
        CellItem item = (CellItem)value;
 
        if(isSelected)
        {
            selectedRenderer.getListCellRendererComponent(list, value, index,isSelected);
            // sometimes the list asks for a dummy selected value for size
            // calculations and this might break the tickering state
            if(index == list.getSelectedIndex())
            {
                if(isTickerEnabled() && selectedRenderer.shouldTickerStart())
                {
                    if(!selectedRenderer.isTickerRunning())
                    {
                        parentList = list;
                        list.getComponentForm().registerAnimated(this);
                        selectedRenderer.startTicker(UIManager.getInstance().
                        getLookAndFeel().getTickerSpeed(), true);
                    }
                }
                else
                {
                    if(selectedRenderer.isTickerRunning())
                    {
                        selectedRenderer.stopTicker();
                    }
                }
            }

            //Устанавливаем картинку
            if (item != null)
            {
                selectedRenderer.setIcon(item.getIcon());
                selectedRenderer.getStyle().setFont(item.getStyle().getFont());
                selectedRenderer.getSelectedStyle().setFont(item.getSelectedStyle().getFont());
            }

            return selectedRenderer;
        }
        else
        {
            Label element = (Label)super.getListCellRendererComponent(list,value,index,isSelected);

             //Устанавливаем картинку
            if (item != null)
            {
                element.setIcon(item.getIcon());
                element.getStyle().setFont(item.getStyle().getFont());
                element.getSelectedStyle().setFont(item.getSelectedStyle().getFont());
            }

            return element;//super.getListCellRendererComponent(list,value,index,isSelected);
        }
    }
}
